package com.jvxb.manage.livable.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jvxb.common.utils.CommonUtil;
import com.jvxb.common.utils.EncrypDESUtil;
import com.jvxb.manage.livable.entity.SysRole;
import com.jvxb.manage.livable.entity.SysUser;
import com.jvxb.manage.livable.mapper.SysUserMapper;
import com.jvxb.manage.livable.service.SysRoleService;
import com.jvxb.manage.livable.service.SysUserService;
import com.jvxb.manage.livable.service.WorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 系统用户表 服务实现类
 * </p>
 *
 * @author jvxb
 * @since 2020-06-07
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private SysRoleService roleService;
    @Autowired
    WorkOrderService workOrderService;

    @Override
    public boolean save(SysUser user) {
        try {
            user.setPassword(new EncrypDESUtil().encrypt(user.getPassword()));
        } catch (Exception e) {
            throw new RuntimeException("加密密码失败");
        }
        return baseMapper.insert(user) == 1 ? true : false;
    }

    @Override
    public boolean saveOrUpdate(SysUser user) {
        if (CommonUtil.notNullOrEmpty(user.getRoleId())) {
            String roleName = roleService.getOne(new QueryWrapper<SysRole>().select(SysRole.ROLE_NAME).eq(SysRole.ROLE_ID, user.getRoleId())).getRoleName();
            user.setRoleName(roleName);
        }
        return super.saveOrUpdate(user);
    }
}
